#!/usr/bin/env bash
# vim: noai:ts=4:sw=4:expandtab
#
# popy: Install Polybar and/or Pywal on Arch Linux + i3
# http://terminalroot.com.br/shell
# http://gitlab.com/terminalroot/popy
#
# The MIT License (MIT)
#
# Copyright (c) 2018 Marcos Oliveira
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

shopt -s extglob

usage() {
  cat <<EOF

usage: ${0##*/} [flags]

  Options:

    --pywal        Only install pywal
    --polybar      Only install polybar
    --all          Install all
    --nocompile    Use package
    --version,-v   Show version
    --help,-h      Show this is message

* The --nocompile option is still under development

EOF
}


if [[ -z $1 || $1 = @(-h|--help) ]]; then
  usage
  exit $(( $# ? 0 : 1 ))
fi

version="${0##*/} version 1.1"

get_deps(){

    [[ "$1" -eq "0" ]] && local all_deps=('python' 'imagemagick' 'python-pip')
    [[ "$1" -eq "1" ]] && local all_deps=('cairo' 'libxcb' 'python2' 'xcb-proto' 'xcb-util-image' 'xcb-util-wm' 'xcb-util-cursor' 'xcb-util-xrm' 'alsa-lib' 'libpulse' 'jsoncpp' 'libmpdclient' 'curl' 'cmake' 'make' 'base-devel' 'python2' 'powerline-fonts' 'libmpdclient' 'git')

    local startin=0
    local deps_1=
    
    while [[ "$startin" -lt "${#all_deps[@]}" ]]; do
    
        if [[ ! "$(which ${all_deps[$startin]} 2>&-)" ]]; then
            local Z="${all_deps[$startin]}"
            deps_1+="$Z "
        fi        
        
        let startin=startin+1
        
    
    done
    
    local deps_1=$(echo $deps_1 | sed 's/ $//')
    echo $deps_1

}

set_pywal(){


    sudo pacman -S $(get_deps 0) --noconfirm
    sudo pip3 install pywal
    
    if [[ "$(ls /usr/share/pixmaps/back.jpg 2>/dev/null)" ]];then
        wal -i /usr/share/pixmaps/back.jpg
    else
        wal -i ~/Wallpapers/01.png
    fi
    echo '(cat ~/.cache/wal/sequences &)' >> /home/$USER/.bashrc

}

set_polybar(){

    cp launch.sh /tmp/launch.sh
    sudo pacman -S $(get_deps 1) --noconfirm
    git clone https://aur.archlinux.org/polybar-git.git
    cd polybar-git/
    makepkg
    sudo pacman -U polybar-git-3* --noconfirm
    mkdir ~/.config/polybar/
    cp /usr/share/doc/polybar/config ~/.config/polybar/
    cp /tmp/launch.sh /home/$USER/.config/polybar/launch.sh
    chmod +x /home/$USER/.config/polybar/launch.sh
    echo 'exec_always --no-startup-id $HOME/.config/polybar/launch.sh' >> /home/$USER/.config/i3/config
    fc-cache -fv
    i3-msg restart
    
}

while [[ "$1" ]]; do
        case "$1" in
            "--pywal") set_pywal ;;
            "--polybar") set_polybar ;;
            "--all") set_pywal && set_polybar ;;
            "--nocompile") usage | tail -n 2 ;;
            "--help"|"-h") usage ;;
            "--version"|"-v") printf "%s\n" "$version" ;;
            *) echo 'Invalid option.' && usage && exit 1 ;;
        esac
        shift
done
